package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.Employee;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users2")
public class UsersController {
    @Autowired
    private UserService userService;

    @GetMapping("/getall")
    public PageInfo<Employee> getall(Employee employee,int page,int size){
        PageHelper.startPage(page,size);
        List<Employee> getall = userService.getall(employee.getRealname());
        return PageInfo.of(getall);
    }

    @GetMapping("/add")
    public String add(){
        return "/user/add";
    }

    @PostMapping("/add")
    public int add(@RequestBody Employee employee){
        int count = userService.add(employee);
        return count;
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Long id){
        return userService.del(id);
    }

    @GetMapping("/{id}")
    public Employee get(@PathVariable Long id){
        return userService.get(id);
    }

    @PutMapping
    public int update(@RequestBody Employee employee){
        return userService.update(employee);
    }

    @PostMapping("/get")
    public PageInfo<Employee> get(@RequestBody Employee employee,int page,int size){
        PageHelper.startPage(page,size);
        List<Employee> getall = userService.getall(employee.getRealname());
        return PageInfo.of(getall);
    }
}
